<template>
  <div class="ordersBox">
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="box-card">
      <el-row type="flex">
        <el-col :span="8">
          <div class="grid-content bg-purple" style="text-align: left">
            <el-input
              clearable
              placeholder="请输入内容"
              class="input-with-select"
              v-model.trim="params.query"
              @clear="getSearchOrders"
              @keydown.native.enter="getSearchOrders"
            >
              <el-button
                slot="append"
                icon="el-icon-search"
                @click="getSearchOrders"
              ></el-button>
            </el-input>
          </div>
        </el-col>
      </el-row>

      <el-table :data="tableData" border style="width: 100%  margin-top:10px">
        <el-table-column type="index" width="50"> </el-table-column>
        <!-- prop为对应列内容的字段名 -->
        <el-table-column
          prop="order_number"
          label="订单编号"
          width="200"
          align="center"
        >
        </el-table-column>
        <el-table-column prop="order_price" label="订单价格" align="center">
        </el-table-column>
        <el-table-column prop="pay_status" label="是否付款" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="success"
              v-if="scope.row.pay_status == '1'"
              >已付款</el-button
            >
            <el-button size="mini" type="danger" v-else>未付款</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货" align="center">
        </el-table-column>
        <el-table-column prop="create_time" label="下单时间" align="center">
          <template slot-scope="scope">
            {{ scope.row.create_time | formDate }}
          </template>
        </el-table-column>

        <el-table-column
          prop="mg_state"
          label="操作"
          width="180"
          align="center"
        >
          <template slot-scope="scope">
            <el-tooltip
              class="item"
              effect="dark"
              content="修改地址"
              placement="top"
            >
              <el-button
                size="mini"
                type="primary"
                icon="el-icon-edit"
                class="operate"
                @click="editAddress(scope.row)"
              ></el-button>
            </el-tooltip>

            <el-tooltip
              class="item"
              effect="dark"
              content="物流信息"
              placement="top"
            >
              <el-button
                size="mini"
                type="danger"
                icon="el-icon-location"
                class="operate"
                @click="handleParcelInformation(scope.$index, scope.row)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="params.pagenum * 1"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="params.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalpage"
      >
      </el-pagination>
    </el-card>

    <!-- 修改地址弹框 -->
    <el-dialog title="收货地址" :visible.sync="addressDialog">
      <el-form :model="addressForm" :rules="rules">
        <el-form-item
          label="省市区/县"
          prop="address1"
          :label-width="formLabelWidth"
        >
          <el-cascader
            v-model="addressForm.address1"
            :options="cityData"
            :props="{ expandTrigger: 'hover' }"
            @change="handleChange"
          ></el-cascader>
        </el-form-item>

        <el-form-item
          label="详细地址"
          prop="address2"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="addressForm.address2"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelDecidedAddress">取 消</el-button>
        <el-button type="primary" @click="decidedAddress">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 物流信息弹框 -->
    <el-dialog title="物流进度" :visible.sync="packageDialog">
      <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in packageList"
          :key="index"
          :icon="activity.icon"
          :type="activity.type"
          :color="activity.color"
          :timestamp="activity.ftime"
        >
          {{ activity.context }}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import { getOrders, getPackageInformation } from "../../api/index";
import cityData from "../../views/orders/cityData";
export default {
  data() {
    return {
      tableData: [],
      totalpage: 0, //总页数
      // 分页数据
      params: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },
      addressDialog: false,
      packageDialog: false,
      formLabelWidth: "120px",
      addressForm: {
        address1: "",
        address2: "",
      },
      rules: {
        address1: [
          { required: true, message: "请输入省市区", trigger: "blur" },
        ],
        address2: [
          { required: true, message: "请输入详细地址", trigger: "blur" },
        ],
      },
      cityData,
      packageList:[]
    };
  },
  methods: {
    async initOrdersList() {
      let {
        data: { goods, pagenum, total },
      } = await getOrders(this.params);
      // console.log(data);
      this.tableData = goods;
      this.params.pagenum = pagenum;
      this.totalpage = total;
    },
    getSearchOrders() {
      this.initOrdersList();
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      // 每页显示数据的改变即为向后台发送数据请求,得到相应的页数和数据量然后渲染
      this.params.pagesize = val;
      this.initOrdersList();
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.params.pagenum = val;
      this.initOrdersList();
    },
    handleChange(data) {
      console.log(data);
    },
    cancelDecidedAddress() {
      this.addressDialog = false;
      this.addressForm = {};
    },
    decidedAddress() {
      this.addressDialog = false;
    },
    editAddress() {
      this.addressDialog = true;
      // console.log(this.cityData);
    },
    async handleParcelInformation() {
      this.packageDialog = true;
      let { data, meta: { msg, status }} = await getPackageInformation({ id: 1106975712662 })
      // console.log(data);
      if (status != 200) return this.$message.error(msg)
      let obj = { type: "success", icon: "el-icon-location-outline", size: 'large' }
      // 定义最新数据的节点样式
      // data[data.length - 1].color = "#008c8c";
      // data[data.length - 1].icon = "el-icon-location-outline";
      data[0] = {...data[0], ...obj}

      this.packageList = data
    },
  },
  created() {
    this.initOrdersList();
  },
};
</script>

<style scoped>
.box-card {
  width: 100%;
  max-height: 820px;
  overflow: auto;
}

.ordersBox {
  padding-left: 20px;
}

.el-breadcrumb {
  font-size: 14px;
  background-color: initial;
  margin-bottom: 0;
}

.el-table {
  margin-top: 20px;
  max-height: 650px;
  overflow: auto;
}
</style>
